﻿using System;
using Coolite.Ext.Web;
using Pap.Web.Modules.Administration.ViewModel;
using Pap.Web.Utils;
using System.Configuration;

namespace Pap.Web.Modules.Administration.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class DatabaseManagementUI : BasePage<DatabaseManagementUIViewModel>
    {
        private string DBBackupRstorePath
        {
            get { return ConfigurationManager.AppSettings["DBBackupRstoreLocation"]; }
        }
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Handles the Click event of the btnBackUp control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void btnBackUp_Click(object sender, AjaxEventArgs e)
        {
            try
            {
                this.ViewModel.BackUpDatabase(DBBackupRstorePath);
                Ext.Msg.Configure(new MessageBox.Config
                {
                    Title = "Success",
                    Message = "Database is backed up successfuly.",
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO
                }).Show();
            }
            catch (Exception ex)
            {
                Ext.Msg.Configure(new MessageBox.Config
                {
                    Title = "Error",
                    Message = "An error happend while backing up the database. -- " + ex.Message,
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.ERROR
                }).Show();
            }
        }

        /// <summary>
        /// Handles the Click event of the btnRestore control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void btnRestore_Click(object sender, AjaxEventArgs e)
        {
            try
            {
                this.ViewModel.RestoreDatabase(DBBackupRstorePath);
                Ext.Msg.Configure(new MessageBox.Config
                {
                    Title = "Success",
                    Message = "Database is restored successfuly.",
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO
                }).Show();
            }
            catch (Exception ex)
            {
                Ext.Msg.Configure(new MessageBox.Config
                {
                    Title = "Error",
                    Message = "An error happend while restoring the database. -- " + ex.Message,
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.ERROR
                }).Show();
            }
        }
    }
}
